package com.bw.journal.insert

import android.text.TextUtils
import android.view.LayoutInflater
import androidx.activity.viewModels
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.lifecycleScope
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.common.mvi.BaseActivity
import com.bw.journal.R
import com.bw.journal.databinding.ActivityJournalInsertBinding
import kotlinx.coroutines.launch

@Route(path = "/journal/JournalInsertActivity")
class JournalInsertActivity : BaseActivity<ActivityJournalInsertBinding,JournalInsertViewModel>() {

    var type = 0
    private val fragment = JounalTypeFragment()

    override fun createVM(): JournalInsertViewModel {
        return viewModels<JournalInsertViewModel>().value
    }

    override fun initData() {
    }

    fun refreshView(){
        var tile = ""
        var nowTitle = ""
        var nextTitle = ""
        if (type==1){
            tile = "日报"
            nowTitle = "今日工作总结"
            nextTitle = "明日工作计划"
        }else if (type == 2){
            tile = "周报"
            nowTitle = "本周工作总结"
            nextTitle = "下周工作计划"
        }else if (type == 3){
            tile = "月报"
            nowTitle = "本月工作总结"
            nextTitle = "下月工作计划"
        }
        v.journalInsertTitle.text = tile
        v.journalInsertNowTitle.text = nowTitle
        v.journalInsertNextTitle.text = nextTitle
    }

    override fun initView() {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
        transaction.replace(R.id.journal_insert_frame,fragment).commit()
        v.journalInsertPage.backImg.setOnClickListener { finish() }
        v.journalInsertPage.titleTv.text = "工作汇报"
        v.journalInsertSub.setOnClickListener {
            if (TextUtils.isEmpty(v.journalInsertNowValue.text)){
                ToastUtils.showShort("请完整填写计划")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(v.journalInsertNextValue.text)){
                ToastUtils.showShort("请完整填写计划")
                return@setOnClickListener
            }
            val map = HashMap<String,Any>()
            map["reportType"]=type
            map["reportNowTitle"]=v.journalInsertNowValue.text.toString()
            map["reportNowValue"]=v.journalInsertNowValue.text.toString()
            map["reportNextTitle"]=v.journalInsertNextValue.text.toString()
            map["reportNextValue"]=v.journalInsertNextValue.text.toString()
            map["reportRemind"]="王女士"
            map["reportTime"]=System.currentTimeMillis()/1000
            vm.insert(map)
        }

    }

    override fun bindState() {
        lifecycleScope.launch {
            vm.mutableContainer.uiStateFlow.collect{
                if (it.journalState){
                    ToastUtils.showShort("提交成功")
                    finish()
                }
            }
        }
    }

    override fun createViewBinding(): ViewBinding {
        return ActivityJournalInsertBinding.inflate(LayoutInflater.from(this))
    }
}